package com.example.demo.service;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserFollowsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserFollowsService {
    @Autowired
    private UserFollowsMapper userFollowsMapper;

    // 关注用户
    public Map<String, Object> followUser(Integer followerId, Integer followedId) {
        Map<String, Object> result = new HashMap<>();
        try {
            userFollowsMapper.followUser(followerId, followedId);
            result.put("code", 200);
            result.put("message", "关注成功");
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "关注失败: " + e.getMessage());
        }
        return result;
    }

    // 取消关注用户
    public Map<String, Object> unfollowUser(Integer followerId, Integer followedId) {
        Map<String, Object> result = new HashMap<>();
        try {
            userFollowsMapper.unfollowUser(followerId, followedId);
            result.put("code", 200);
            result.put("message", "取消关注成功");
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "取消关注失败: " + e.getMessage());
        }
        return result;
    }

    // 获取关注者数量
    public int getFollowerCount(Integer userId) {
        return userFollowsMapper.getFollowerCount(userId);
    }

    // 获取关注者列表
    public List<User> getFollowers(Long userId) {
        return userFollowsMapper.getFollowers(userId);
    }

    // 获取当前用户关注的人的数量
    public int getFollowedCount(Integer followerId) {
        return userFollowsMapper.getFollowedCount(followerId);
    }


    // 获取当前用户关注的人的列表
    public List<User> getFollowedUsers(Long followerId) {
        return userFollowsMapper.getFollowedUsers(followerId);
    }
//    判断是否已经关注
    public Map<String, Object> checkFollowStatus(Integer followerId, Integer followedId) {
        Map<String, Object> result = new HashMap<>();
        try {
            int count = userFollowsMapper.isFollowing(followerId, followedId);
            result.put("code", 200);
            result.put("message", "successful");
            result.put("isFollowing", count > 0); // 如果 count > 0，表示已关注
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "检查关注状态失败: " + e.getMessage());
        }
        return result;
    }

}

